﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using TrinityAPI.Base;
using TrinityAPI.Base.Tools;
using AutoMailLib;

namespace TrinityAPI.AutoMail
{
    public class GetTasks : IAction
    {
        public JToken Invoke(JObject dataset, params JToken[] args)
        {
            JObject result = new JObject();
            var request = dataset.GetJsonObject("request");
            var constant = dataset.GetJsonObject("constant");
            var type = request.GetJsonObjectIntValue("type");
            var remoteuri = constant.GetNecessaryJsonObjectStringValue("remoteuri", "constant");
            DateTime time = DateTime.Now;
            var email = Common.GetEmailObj(remoteuri);
            LogHelper.Info(string.Format("Get Auto Email Tasks Take Times: {0}ms", (DateTime.Now - time).TotalMilliseconds));
            if (email != null)
            {
                string key = constant.GetNecessaryJsonObjectStringValue("adminmodekey", "constant");
                JArray running = new JArray();
                JArray outstanding = new JArray();
                if (type == 0 || type == 1)
                {
                    time = DateTime.Now;
                    List<RptDocumentInfo> runningList = email.GetLstRptDocInfoes();
                    LogHelper.Info(string.Format("Get Auto Email Tasks Take Times(GetLstRptDocInfoes): {0}ms", (DateTime.Now - time).TotalMilliseconds));
                    if (runningList != null && runningList.Count > 0)
                    {
                        foreach (var item in runningList)
                        {
                            JObject obj = JObject.FromObject(item);
                            obj["Duration"] = (int)(DateTime.Now - item.StartTime).TotalSeconds;
                            obj["CHANNEL"] = string.IsNullOrEmpty(item.SubsID) ? string.Empty : Utils.MD5(item.SubsID + key);
                            running.Add(obj);
                        }
                    }
                }
                if (type == 0 || type == 2)
                {
                    time = DateTime.Now;
                    List<RptDocumentInfo> outstandingList = email.GetRptDocumentInfos();
                    LogHelper.Info(string.Format("Get Auto Email Tasks Take Times(GetRptDocumentInfos): {0}ms", (DateTime.Now - time).TotalMilliseconds));
                    if (outstandingList != null && outstandingList.Count > 0)
                    {
                        foreach (var item in outstandingList)
                        {
                            JObject obj = JObject.FromObject(item);
                            obj["Duration"] = (int)(DateTime.Now - item.CreateTime).TotalSeconds;
                            obj["CHANNEL"] = string.IsNullOrEmpty(item.SubsID) ? string.Empty : Utils.MD5(item.SubsID + key);
                            outstanding.Add(obj);
                        }
                    }
                }

                result.Add("running", running);
                result.Add("outstanding", outstanding);
            }
            return result;
        }
    }
}
